package com.example.sync;

public class DeadLock {

	static Object a = new Object(); 
	static Object b = new Object(); 
	
	public static void main(String[] args) throws Exception {
		new Thread(() -> {
			synchronized (a) {
				System.out.println(Thread.currentThread().getName() + "获取到锁a，等待获取锁b");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				synchronized (b) {
					System.out.println(Thread.currentThread().getName() + "获取到全部锁");
				}
			}
		}, "AA").start();
		
		Thread.sleep(10);
		
		new Thread(() -> {
			synchronized (b) {
				System.out.println(Thread.currentThread().getName() + "获取到锁b，等待获取锁a");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				synchronized (a) {
					System.out.println(Thread.currentThread().getName() + "获取到全部锁");
				}
			}
		}, "BB").start();
	}
}
